﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ListNetRanker
{
    public class DataReader
    {
        /** The samples. */
        private List<Sample> sampleList;

        /** The cur. */
        private int cur = 0;

        public DataReader()
        {


        }

        public void LoadSamplesFromFile(string strFileName)
        {
            StreamReader sr = new StreamReader(strFileName);
            sampleList = new List<Sample>();

            Sample sample = null;
            while (sr.EndOfStream == false)
            {
                string strLine = sr.ReadLine();
                if (strLine.StartsWith("#") == true)
                {
                    continue;
                }

                Document doc = Document.Parse(strLine);
                if (sample == null)
                {
                    sample = new Sample(doc.qid);
                }
                else if (sample.qid != doc.qid)
                {
                    sampleList.Add(sample);
                    sample = new Sample(doc.qid);
                }

                sample.add(doc);
            }

            if (sample != null)
            {
                sampleList.Add(sample);
            }
            sr.Close();
        }



        public Sample getNextSample()
        {
            if (cur >= this.sampleList.Count)
                return null;

            Sample s = this.sampleList[cur];
            cur++;
            return s;
        }


        public void reset()
        {
            cur = 0;
        }

    }
}
